import { constantRoutes, asyncRoutes } from '@/router'

const getDefaultState = () => {
  return {
    routes: [...constantRoutes]
  }
}

const state = getDefaultState()

const mutations = {
  setRoutes(state, routes) {
    state.routes = [...constantRoutes, ...routes]
  }
}

const actions = {
  getRoutes(context, menus) {
    const routes = asyncRoutes.filter(v => menus.includes(v.name))
    routes.push({ path: '*', redirect: '/404', hidden: true })
    context.commit('setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
